import re

def clean_text(text):
    # 去除多余的空格和换行符
    # text = re.sub(r' ', '', text).strip()
    # 按行分割文本
    lines = text.splitlines()
    cleaned_lines = []

    for line in lines:
        # 删除每一行末尾的标点符号和数字
        cleaned_line = re.sub(r'[ 、。.，；!?,]*\s*\d*\s*$', '', line)
        # 如果清理后的行不是空行，则保留
        if cleaned_line:
            cleaned_lines.append(cleaned_line)

    cleaned_lines.append('')

    # 将清理后的行重新组合成一个字符串
    cleaned_text = '\n'.join(cleaned_lines)
    return cleaned_text

if __name__ == '__main__':
    # 指定目录文件路径
    input_contents_path = "tmp_txt/01_contents/04_银河麒麟桌面操作系统V10 SP1 2503产品用户手册.txt"
    with open(input_contents_path, "r", encoding="utf-8") as doc:
        input_contents = doc.read()

    output_header_path = "tmp_txt/02_clean/contents/04_银河麒麟桌面操作系统V10 SP1 2503产品用户手册.txt"

    # 清理文本
    cleaned_header_text = clean_text(input_contents)

    # 保存清洗文本（header）
    with open(output_header_path, "w", encoding="utf-8") as output_file:
            output_file.write(cleaned_header_text)

    # 输出清洗后的标题
    print(f"清洗后的标题已成功保存到 {output_header_path}")


